package com.example.order.controller.api;

import com.example.common.util.core.controller.BaseController;
import com.example.common.util.core.resultbody.AjaxResult;
import com.example.common.util.enums.OrderStatusEnums;
import com.example.model.domain.entity.order.OrderInfo;
import com.example.model.vo.order.OrderCountQueryVo;
import com.example.model.vo.order.OrderQueryVo;
import com.example.order.service.OrderInfoService;
import com.example.service.util.conversion.OrderInfoConversion;
import com.example.service.util.service.TokenService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Author juzipi
 * @Date 2021/7/23 20:52
 * @Info
 */
@Api(tags = "前台订单管理")
@RestController
@RequestMapping("api/order/order")
public class OrderInfoApiController extends BaseController {


    @Autowired
    private OrderInfoService orderInfoService;

    @Autowired
    private TokenService tokenService;


    /**
     * 生成订单
     * @param hosScheduleId
     * @param patientId
     * @return
     */
    @ApiOperation("生成订单")
    @GetMapping("auth/add/{hosScheduleId}/{patientId}")
    public AjaxResult addOrders(@PathVariable String hosScheduleId, @PathVariable Long patientId)
    {
        Long orderId = orderInfoService.saveOrders(hosScheduleId,patientId);
        return AjaxResult.success(orderId);
    }


    /**
     * 根据订单id查询订单
     */
    @ApiOperation("根据订单id查询订单")
    @GetMapping(value = "get/{orderId}")
    public AjaxResult getOrderInfo(@PathVariable("orderId") Long orderId)
    {
        return AjaxResult.success(orderInfoService.selectOrderInfoById(orderId));
    }


    //订单列表（条件查询带分页）
    @ApiOperation("查询订单分页")
    @GetMapping("auth/page/{pageNum}/{pageSize}")
    public AjaxResult getOrderInfoPage(
            @PathVariable Integer pageNum,
            @PathVariable Integer pageSize,
            OrderQueryVo orderQueryVo,
            HttpServletRequest request) {
        //设置当前用户id
        orderQueryVo.setUserId(tokenService.getUserId(request));
        PageInfo<OrderInfo> orderInfoPage = orderInfoService.getOrderInfoPage(pageNum, pageSize, orderQueryVo);
        return AjaxResult.success(orderInfoPage);
    }


    @ApiOperation(value = "获取订单状态")
    @GetMapping("auth/status")
    public AjaxResult getStatusList() {
//        List<Map<String, Object>> statusList = OrderInfoConversion.getStatusList();
        List<Map<String, Object>> statusList = OrderStatusEnums.getStatusList();
        return AjaxResult.success(statusList);
    }


    @ApiOperation(value = "取消预约")
    @GetMapping("auth/cancel/{orderId}")
    public AjaxResult cancelOrder(@PathVariable Long orderId) {
        Boolean aBoolean = orderInfoService.cancelOrderById(orderId);
        return toAjax(aBoolean);
    }


    //获取订单统计数据
    @ApiOperation("获取订单统计数据")
    @PostMapping("inner/count")
    public AjaxResult getCountMap(@RequestBody OrderCountQueryVo orderCountQueryVo) {
        Map<String, Object> orderInfoCountMap = orderInfoService.getOrderInfoCountMap(orderCountQueryVo);
        return AjaxResult.success(orderInfoCountMap);
    }


}
